package com.idas.common;

import java.security.MessageDigest;
import java.util.Random;
import java.util.UUID;

import com.idas.comp.enums.ResourceType;

public class Util {

	public static String generateResourceUUID(ResourceType resource_type,
			long resource_id, long user_id) {
		if (resource_id == 0) {
			resource_id = 1000 + (int) (Math.random() * ((10000 - 1000) + 1));
		}

		try {
			String to_hash = "Napier" + resource_type + "-" + resource_id + "-"
					+ user_id + System.currentTimeMillis();
			byte[] msg_bytes = to_hash.getBytes();
			MessageDigest md = MessageDigest.getInstance("MD5");
			md.reset();
			md.update(msg_bytes);
			byte[] for_uuid = md.digest();

			UUID uuid = UUID.nameUUIDFromBytes(for_uuid);

			return uuid.toString();

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	public static String rand64Hex() {
		Random generator = new Random();
		String hexstr = "";
		for (int i = 0; i < 8; i++) {
			int num = generator.nextInt();
			String str = Integer.toHexString(num);
			int len = str.length();
			if (len < 8) {
				for (int x = 0; x < (8 - len); x++) {
					str += "0";
				}
			}
			hexstr += str;
		}
		return hexstr;
	}

	public static boolean isNotNull(String arg) {
		return (null != arg && !"".equals(arg.trim()));
	}

	public static boolean vaildateTimestamp(long timestamp) {
		long currTime = System.currentTimeMillis() / 1000;
		if ((currTime - timestamp) < 0) {
			return false;
		}
		return true;
	}

	public static boolean requiresUpdate(String currArg, String newArg) {
		if (!isNotNull(newArg)) {
			if (currArg.equals(newArg)) {
				return true;
			}
		}
		return false;
	}

}
